<template>  
	<div>
		<view class="form_list">
			<view class="input2">
				<uni-easyinput prefixIcon="scan" trim disabled 
					:value="scanCode" placeholderStyle="font-size:17px;" ></uni-easyinput> 
			</view>
		</view>
		<view class="form_list">
			<view class="label">物料编码</view>
			<view class="input2">
				<text>{{material.FNumber}}</text>
			</view>
		</view>
		<view class="form_list">
			<view class="label">物料名称</view>
			<view class="input2">
				<text>{{material.FName}}</text>
			</view>
		</view>
		<view class="form_list">
			<view class="label">规格</view>
			<view class="input2">
				<text>{{material.FModel}}</text>
			</view>
		</view>
		<!-- <view class="form_list">
			<view class="label">净重</view>
			<view class="input2">
				<text>{{item.FNetWeight}}</text>
			</view>
		</view> -->
		<view class="form_list">
			<view class="label">单位</view>
			<view class="input2">
				<text>{{material.FUnit_Name}}</text>
			</view>
		</view>
		<view class="form_list">
			<view class="label">批号</view>
			<view class="input2">
				<text>{{material.FBatchNo}}</text>
			</view>
		</view>
		<view class="form_list">
			<view class="label">箱头</view>
			<view class="input2">
				<text>{{material.FAuxPropNumber}}</text>
			</view>
		</view>
		<view class="form_list" v-show="checkInvFlag">
			<view class="label">账面数量</view>
			<view class="input2">
				<text>{{FStockQty}}</text>
			</view>
		</view>
	</div>
</template>  

<script>  
var main,receiver,filter;    
var _codeQueryTag = false;
import {
		uniEasyinput
	} from '@dcloudio/uni-ui'
	
export default {  
	components: {
		uniEasyinput
	},
	props: {
		//默认检查库存
		checkInvFlag:{
			type:Boolean,
			default:true
		}
	},
    data() {  
        return {  
            scanCode: '',
			material:{},
			FStockQty:''
        }  
    },  
    created: function (option) {  
        this.initScan()  
        this.startScan();    
    },    
    onHide:function(){    
        this.stopScan();    
    },  
    destroyed:function(){    
        /*页面退出时一定要卸载监听,否则下次进来时会重复，造成扫一次出2个以上的结果*/    
        this.stopScan();    
    },
	mounted() {
		let _this = this;  
		uni.$on('clearScanMaterial',function(flag){
			_this.material = {}
			_this.FStockQty = ''
			if(flag){
				_this.$Api.msg('操作成功','success')
			}
		})
	},
    methods: {  
        initScan() {  
            let _this = this;  
            main = plus.android.runtimeMainActivity();//获取activity  
            var IntentFilter = plus.android.importClass('android.content.IntentFilter');   
            filter = new IntentFilter();    
            filter.addAction("android.intent.ACTION_DECODE_DATA"); // 广播动作  
            receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver',{  
            onReceive : function(context, intent) {  
                plus.android.importClass(intent);     
                let code = intent.getStringExtra("barcode_string");// 广播标签  
                _this.queryCode(code);    
            }});   
        },    
        startScan(){    
            main.registerReceiver(receiver,filter);  
        },    
        stopScan(){  
            main.unregisterReceiver(receiver);    
        },    
        queryCode: function(code){  
            //防重复  
            if(_codeQueryTag)return false;    
            _codeQueryTag = true;    
            setTimeout(function(){    
                _codeQueryTag = false;    
            },150);  
            //uni.$emit('scancodedate',{code:code})
			this.getMateriel(code)
        },
		//获得物料
		async getMateriel(e) {
			let _this = this;
			var eArr = e.split('-');
			if (eArr.length == 4) {
				let material_id = Number(eArr[0]);
				if (material_id > 0) {
					await this.$Db.selectTableData('material', 'FItemID', material_id).then(res => {
						if (res[0] == undefined) {
							_this.$Api.msg('条码异常','error');
							return 
						}
						_this.material = res[0]
					});
					//批号，箱头
					this.material.FBatchNo = eArr[1];
					this.material.fweight = eArr[2]; //净重
					this.material.FAuxPropNumber = eArr[3];
					
					//检查库存
					if(this.checkInvFlag){
						let condition = {
									'FItemID':material_id,
									'FBatchNo':eArr[1],
									'FAuxPropNumber':eArr[3]
						};
						await this.$Db.selectDataList('inventory', condition).then(res => {
							if (res[0] == undefined) {
								//找不到库存记录
								this.FStockQty = 0
							}else{
								this.FStockQty = res[0].FQty
							}
							this.material.FStockQty = this.FStockQty
						});
					}
					
					uni.vibrateLong()//长震动
					uni.stopPullDownRefresh()
					uni.$emit('scanMaterial',_this.material)//{material:_this.material}
				}
			}else{
				this.$Api.msg('条码异常','error');
			}
		},
    }  
}  
</script>  